/**
 * 
 */
package com.cdhospital.admincontroller;

import java.util.Date;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.cdhospital.Model.User;
import com.cdhospital.adminservice.LoginService;
import com.jfinal.core.Controller;

import io.goeasy.GoEasy;

/**
 * @author Administrator
 *
 */
public class AdminIndexController extends Controller {
	 static LoginService service=new LoginService();
	public void index(){
		System.out.println(getSession().getAttribute("username"));
		if(getSession().getAttribute("username")==null){
			redirect("/admin/login");
		}
		else
			render("index.html");
	}
	public void upmm(){
		render("mm.html");
	}
	public void weixin() throws DocumentException{
		String username=getSession().getAttribute("username").toString();
		  User user=service.getUserByName(username);
		  if(user.getWeixinid()!=null){
			  setAttr("status", 1);
		  }
		  else{
			//发送 GET 请求
			    String s=ss.sendGet("https://login.weixin.qq.com/jslogin", "fun=new&lang=zh_CN&appid=wx782c26e4c19acffb");
			    s=s.substring(s.indexOf("\"")+1, s.lastIndexOf("\""));
			  String erweima="https://login.weixin.qq.com/qrcode/"+s;
			   setAttr("ma", erweima);
			   setAttr("s", s);
			   setAttr("status", 2);
		  }
		
		render("weixin.html");
	}
	
	public void listen() throws DocumentException{
		String s=getPara("s");
		  //获取二维码少秒状态   get
	      //https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=gd94hc3_fg==&tip=0&r=-1160587432&_=1452859503803
	      String satuts=null;
	      boolean flag=true;
	      String date=new Date().getTime()+"";
	      while(flag){
	    	 	 satuts=ss.sendGet("https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login", "loginicon=true&uuid="+s+"&tip=0&r=-"+date+""+"&_=1452859503803"); 
	    	     String  satut=satuts.substring(satuts.indexOf("=")+1, satuts.indexOf(";"));
	    	     if(satut.equals("200")){
	    	    	 System.err.println("登录成功");
	    	    	 setAttr("err", 1);
	    	    	 flag=false;
	    	    	break;
	    	     }
	    	     else if(satut.equals("201")){
	    	    	 System.err.println("正在扫描");
	    	     }
	    	     else if(satut.equals("408")){
	    	    	 setAttr("err", 2);
	    	    	 System.err.println("已经超时");
	    	    	 flag=true;
	    	    	 break;
	    	     	
	    	     }
	    	  }
	    if(!flag){
	     satuts=satuts.substring(satuts.indexOf("\"")+1, satuts.lastIndexOf("\""));
	      String ticket=satuts.substring(satuts.indexOf("ticket")+7, satuts.indexOf("uuid")-1);
	      String uuid=satuts.substring(satuts.indexOf("uuid")+5, satuts.indexOf("lang")-1);
	      
	    String message= ss. sendGet("https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage", "ticket="+ticket+"&uuid="+uuid+"&lang=zh_CN&scan="+date+"&fun=new");
	    Document dom=DocumentHelper.parseText(message);
	    Element root=dom.getRootElement();
	    String skey=root.element("skey").getText();
	    String wxsid=root.element("wxsid").getText();
	    String pass_ticket=root.element("pass_ticket").getText();
	    String wxuin=root.element("wxuin").getText();
	     Integer a=Integer.parseInt(wxuin);
	   String username=getSession().getAttribute("username").toString();
	   User user=service.getUserByName(username);
	   user.setWeixinid(a);
	   user.update();
	   renderJson();
	    }
	    else{
	    	setAttr("err", 2);
	    	 System.err.println("已经超时");
	    	 renderJson();
	    }
	}
	public void out(){
		 String username=getSession().getAttribute("username").toString();
		  User user=service.getUserByName(username);
		   user.setWeixinid(null);
		   user.update();
		   redirect("/admin/weixin");
	}
	
}
